package com.cn.web;

import com.cn.entity.CustomerInfo;
import com.cn.service.CustomerInfoService;
import com.cn.service.DeviceErrorService;
import com.cn.service.RunStatusReportService;
import com.cn.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by chenyuncong on 2019/3/27.
 */
//@RestController
//@RequestMapping("data")
public class DataWeb {

    @Autowired
    private DeviceErrorService deviceErrorService;

    @Autowired
    private RunStatusReportService runStatusReportService;

    @Autowired
    private CustomerInfoService customerInfoService;

    @RequestMapping("setStatusData")
    public Map<String, Object> setStatusData(String mac, String startTime, String endTime, int type) throws Exception {
        startTime += " 00:00:00";
        endTime += " 00:00:00";
        runStatusReportService.getRunStatusTemplateList(mac, startTime, endTime, type);
        return null;
    }

    @RequestMapping("md5totxt")
    public String md5totxt() {
        try {
//            String sql = "select MD5(phone) phone from t_customer_info where 1=1 and phone REGEXP \"^[1][3456789][0-9]{9}$\"  GROUP BY phone";
            String sql = "select MD5(mobile) phone from customer_credit_apply where mobile REGEXP \"^[1][3456789][0-9]{9}$\" group by mobile";
            List<CustomerInfo> list = customerInfoService.getList(sql, CustomerInfo.class);
            String psth="D:\\testData\\output2.txt";
            writeFile(psth,list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private static void writeFile(String path, List<CustomerInfo> list) {
        try {
            //"D:\\testData\\output.txt"
            File writeName = new File(path); // 相对路径，如果没有则要建立一个新的output.txt文件
            writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
            try (FileWriter writer = new FileWriter(writeName);
                 BufferedWriter out = new BufferedWriter(writer)
            ) {
                int i=0;
                for(CustomerInfo c:list){
                    out.write(c.getPhone()+"\r\n"); // \r\n即为换行
                    if(i==100){
                        out.flush();
                        i=0;
                    }
                    i++;
                }
//                out.write("我会写入文件啦1\r\n"); // \r\n即为换行
                out.flush(); // 把缓存区内容压入文件
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
